package dimension.starry.mixin;

import net.minecraft.entity.attribute.ClampedEntityAttribute;
import net.minecraft.util.math.MathHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(value = ClampedEntityAttribute.class, priority = 1001)
public class ClampEntityAttributeMixin {

    private static final double ENTITY_MAX_HEALTH = 100 * 100 * 100 * 10;

    @Redirect(method = "clamp", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/MathHelper;clamp(DDD)D"))
    public double onClamp(double value, double min, double max) {
        ClampedEntityAttribute clampedEntityAttribute = (ClampedEntityAttribute) (Object) this;
        if ("attribute.name.generic.max_health".equals(clampedEntityAttribute.getTranslationKey())) {
            return value < min ? min : Math.min(value, ENTITY_MAX_HEALTH);
        } else if ("attribute.name.generic.attack_damage".equals(clampedEntityAttribute.getTranslationKey())) {
            return value < min ? min : Math.min(value, ENTITY_MAX_HEALTH);
        }
        return value < min ? min : Math.min(value, max);
    }

}
